//
//  HomeMainCell.m
//  AnimationRecommend
//
//  Created by ljm on 2017/2/15.
//  Copyright © 2017年 ljm. All rights reserved.
//

#import "HomeMainCell.h"

@interface HomeMainCell ()

@end

@implementation HomeMainCell

#pragma mark -- 自定义cell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        /*
         *    图片
         */
        _iconView = [[UIImageView alloc] init];
        _iconView.translatesAutoresizingMaskIntoConstraints = NO;
        _iconView.contentMode = UIViewContentModeScaleAspectFill;
        _iconView.clipsToBounds = YES;
        [self.contentView addSubview:_iconView];
        
        /*
         *    标题
         */
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
        _titleLabel.font = ManYu_Font;
        [self.contentView addSubview:_titleLabel];
        
        /*
         *    详细内容
         */
        _descLabel = [[UILabel alloc] init];
        _descLabel.translatesAutoresizingMaskIntoConstraints = NO;
        _descLabel.numberOfLines = 0;
        _descLabel.font = [UIFont fontWithName:ManYu_FontStr size:12.f];
        [self.contentView addSubview:_descLabel];
        
        [self setNeedsUpdateConstraints];
    }
    return self;
}


- (void)updateConstraints
{
    /*
     *    图片距离前方的约束
     */
    [self addContraints:@"H:|-20-[_iconView(100)]" forViews:NSDictionaryOfVariableBindings(_iconView)];
    
    /*
     *    图片前后的约束
     */
    [self addContraints:@"V:|-5-[_iconView]-5-|" forViews:NSDictionaryOfVariableBindings(_iconView)];
    
    /*
     *    图片距离标题的距离和标题对后方的约束
     */
    [self addContraints:@"H:[_iconView]-10-[_titleLabel]-10-|" forViews:NSDictionaryOfVariableBindings(_iconView, _titleLabel)];
    NSString *titleContraints = [NSString stringWithFormat:@"V:|-10-[_titleLabel(%f)]", _titleLabel.font.lineHeight];
    [self addContraints:titleContraints forViews:NSDictionaryOfVariableBindings(_titleLabel)];
    
    /*
     *    图片对详细内容前后的横向约束
     */
    [self addContraints:@"H:[_iconView]-15-[_descLabel]-10-|" forViews:NSDictionaryOfVariableBindings(_iconView, _descLabel)];
    
    /*
     *    标题对详细内容的竖向约束
     */
    [self addContraints:@"V:[_titleLabel]-10-[_descLabel]-5-|" forViews:NSDictionaryOfVariableBindings(_titleLabel, _descLabel)];
    
    [super updateConstraints];
}

#pragma mark -- 自定义给约束的方法
- (void)addContraints:(NSString *)constraint forViews:(NSDictionary *)views
{
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:constraint options:0 metrics:nil views:views]];
}


#pragma mark -- 给模型赋值
- (void)setModel:(HomeMainModel *)model
{
    NSURL *urlStr = [NSURL URLWithString:model.thumbnail];
    [_iconView sd_setImageWithURL:urlStr placeholderImage:[UIImage imageNamed:@"backImage.jpeg"]];
    _titleLabel.text = model.title;
    _descLabel.text = model.excerpt;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
